package com.qdxwx.data.service.order;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.mapper.order.OrderExpressMapper;
import com.qdxwx.data.service.user.UserAddressService;
import com.qdxwx.models.order.Order;
import com.qdxwx.models.order.OrderExpress;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class OrderExpressServiceImpl implements OrderExpressService {

    @Autowired
    private OrderExpressMapper orderExpressMapper;
    @Autowired
    private UserAddressService userAddressService;


    //支付成功插入配送订单
    public int createExpress(Order order, User user) {
        OrderExpress orderExpress = orderExpressMapper.selectByOrderNo(order.getOrderNo());
        if(orderExpress != null) return 0;
        UserAddress userAddress = userAddressService.get(order.getAddressId());
        if(userAddress == null) {
            userAddress = new UserAddress();
            userAddress.setUserId(user.getUserId());
            userAddress.setMobile(user.getMobile());
            userAddress.setUserName(user.getUserName());
        }
        orderExpress = new OrderExpress();
        orderExpress.setUserId(userAddress.getUserId());
        orderExpress.setOrderNo(order.getOrderNo());
        orderExpress.setMobile(userAddress.getMobile());
        orderExpress.setUserName(userAddress.getUserName());
        orderExpress.setStatus(0);
        orderExpress.setAddress(userAddress.getProvince() + userAddress.getAddress());
        return insert(orderExpress);
    }

    @Override
    public int updateCombine(long userId1, long userId2) {
        if(userId1 < 1 || userId2 < 1){
            throw new RuntimeException("更新配送订单：参数错误");
        }
        return orderExpressMapper.updateCombine(userId1, userId2);
    }

    @Override
    public OrderExpress get(int id) {
        if (id < 1) return null;
        return orderExpressMapper.selectById(id);
    }

    @Override
    public OrderExpress get(String orderNo) {
        if(TKString.isEmpty(orderNo)) return null;
        return orderExpressMapper.selectByOrderNo(orderNo);
    }

    @Override
    public int insert(OrderExpress orderExpress) {
        orderExpress.setCreateTime(LocalDateTime.now());
        orderExpress.setUpdateTime(LocalDateTime.now());
        return orderExpressMapper.insertOrderExpress(orderExpress);
    }

    @Override
    public int update(OrderExpress orderExpress) {
        if (orderExpress.getId() < 1) return 0;
        orderExpress.setUpdateTime(LocalDateTime.now());
        return orderExpressMapper.updateOrderExpress(orderExpress);
    }

    @Override
    public int delete(int id) {
        if (id < 1) return 0;
        return orderExpressMapper.deleteById(id);
    }

    @Override
    public List<OrderExpress> getList(OrderExpress orderExpress, PageRequest page) {
        return orderExpressMapper.selectList(orderExpress, page);
    }

    @Override
    public long count(OrderExpress orderExpress) {
        return orderExpressMapper.selectCount(orderExpress);
    }
}
